/*
 * Hibiscus is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation; either version 3 of the License,
 * or (at your option) any later version.
 *
 * Hibiscus is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Hibiscus; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 *
 * Copyright (C) 2003 Tetete - http://tetete.hu.
 */
package hu.tetete.hibiscus.edittaxon;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

import hu.tetete.hibiscus.edittaxon.EditTaxonService.EditTaxonServiceBinder;

/**
 * TODO javadoc.
 */
public class EditTaxonAbstractActivity extends Activity {

    /**
     * Logger's Tag.
     */
    static final String TAG = EditTaxonAbstractActivity.class.getName();

    /** TODO javadoc. */
    protected EditTaxonService         editTaxonService;

    /** TODO javadoc. */
    protected boolean                  mBound;

    /** TODO javadoc. */
    private   ServiceConnection        serviceConnection;

    /** Defines callback methods for service binding, passed to bindService(). */
    protected final class EditTaxonServiceConnection implements ServiceConnection {

        private final ServiceConnection embenddedServiceConnection;

        /**
         * @param embenddedServiceConnection TODO javadoc
         */
        EditTaxonServiceConnection(final ServiceConnection embenddedServiceConnection) {
            super();
            this.embenddedServiceConnection = embenddedServiceConnection;
        }

        /**
         * @see android.content.ServiceConnection#onServiceConnected(android.content.ComponentName, android.os.IBinder)
         */
        @Override
        public void onServiceConnected(final ComponentName className,
                final IBinder service) {

            Log.i(TAG, "Service connected");
            // We've bound to LocalService, cast the IBinder and get LocalService instance
            final EditTaxonServiceBinder binder = (EditTaxonServiceBinder) service;
            EditTaxonAbstractActivity.this.editTaxonService = binder.getService();
            EditTaxonAbstractActivity.this.mBound = true;
            if (this.embenddedServiceConnection != null) {
                this.embenddedServiceConnection.onServiceConnected(className, service);
            }
        }

        /**
         * @see android.content.ServiceConnection#onServiceDisconnected(android.content.ComponentName)
         */
        @Override
        public void onServiceDisconnected(final ComponentName name) {
            EditTaxonAbstractActivity.this.mBound = false;
            if (this.embenddedServiceConnection != null) {
                this.embenddedServiceConnection.onServiceDisconnected(name);
            }
        }
    }

    /**
     *
     * @param savedInstanceState {@link Activity#onCreate(Bundle)}
     * @param embeddedServiceConnection TODO javadoc.
     */
    protected void onCreate(final Bundle savedInstanceState, final ServiceConnection embeddedServiceConnection) {
        super.onCreate(savedInstanceState);

        // Bind to NewTaxonService
        final Intent intent = new Intent(this, EditTaxonService.class);
        this.serviceConnection = new EditTaxonServiceConnection(embeddedServiceConnection);
        bindService(intent, this.serviceConnection, Context.BIND_AUTO_CREATE);
    }

    /**
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        onCreate(savedInstanceState, null);
    }

    /**
     * @see android.app.Activity#onDestroy()
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        /* Unbind from the service */
        if (this.mBound) {
            unbindService(this.serviceConnection);
            this.mBound = false;
        }

    }


}
